//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/user"})
@Validated
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public UserController() {
    }

    @PostMapping({"/register"})
    public Result register(@Pattern(
            regexp = "^\\S{5,16}$"
    ) String username, @Pattern(
            regexp = "^\\S{5,16}$"
    ) String password) {
        User u = this.userService.findByUserName(username);
        if (u == null) {
            this.userService.register(username, password);
            return Result.success();
        } else {
            return Result.error("用户名已存在");
        }
    }

    @PostMapping({"/login"})
    public Result<String> login(@Pattern(
            regexp = "^\\S{5,16}$"
    ) String username, @Pattern(
            regexp = "^\\S{5,16}$"
    ) String password) {
        User loginUser = this.userService.findByUserName(username);
        if (loginUser == null) {
            return Result.error("用户名不存在");
        } else if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {
            Map<String, Object> claims = new HashMap();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            this.stringRedisTemplate.opsForValue().set(token, token, 3L, TimeUnit.HOURS);
            return Result.success(token);
        } else {
            return Result.error("密码错误");
        }
    }

    @GetMapping({"/userInfo"})
    public Result<User> userInfo() {
        Map<String, Object> map = (Map)ThreadLocalUtil.get();
        String username = (String)map.get("username");
        User user = this.userService.findByUserName(username);
        return Result.success(user);
    }

    @PutMapping({"/update"})
    public Result update(@RequestBody @Validated User user) {
        this.userService.update(user);
        return Result.success();
    }

    @PatchMapping({"/updateAvatar"})
    public Result updateAvatar(@RequestParam @URL String avatarUrl) {
        this.userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    @PatchMapping({"/updatePwd"})
    public Result updatePwd(@RequestBody Map<String, String> params, @RequestHeader("Authorization") String token) {
        String oldPwd = (String)params.get("old_pwd");
        String newPwd = (String)params.get("new_pwd");
        String rePwd = (String)params.get("re_pwd");
        if (StringUtils.hasLength(oldPwd) && StringUtils.hasLength(newPwd) && StringUtils.hasLength(rePwd)) {
            Map<String, String> map = (Map)ThreadLocalUtil.get();
            String username = (String)map.get("username");
            User loginUser = this.userService.findByUserName(username);
            if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))) {
                return Result.error("原密码不正确");
            } else if (!newPwd.equals(rePwd)) {
                return Result.error("两次填写的新密码不一样");
            } else {
                this.userService.updatePwd(newPwd);
                this.stringRedisTemplate.opsForValue().getOperations().delete(token);
                return Result.success();
            }
        } else {
            return Result.error("缺少必要的参数");
        }
    }
}
